package com.api.shopapi.mapper;


import com.api.shopapi.entity.UserAddress;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserAddressMapper {

    @Select("select  * from user_address")
    List<UserAddress> getList();

    @Insert("insert into user_address " +
            "(" +
            "UserAddressCode," +
            "UserCode," +
            "ReceiveName," +
            "ReceivePhone," +
            "ReceiveAddress," +
            "AreaCode," +
            "AreaName," +
            "AreaNamePath," +
            "DefaultStatus" +
            ")" +
            "values" +
            "(" +
            "#{userAddress.UserAddressCode}," +
            "#{userAddress.UserCode}," +
            "#{userAddress.ReceiveName}," +
            "#{userAddress.ReceivePhone}," +
            "#{userAddress.ReceiveAddress}," +
            "#{userAddress.AreaCode}," +
            "#{userAddress.AreaName}," +
            "#{userAddress.AreaNamePath}," +
            "#{userAddress.DefaultStatus}" +
            ")")
    void  insert(@Param("userAddress") UserAddress userAddress);

    @Select("select * from user_address " +
            "where " +
            "UserAddressCode=#{userAddressCode} ")
    UserAddress getByUserAddressCode(@Param("userAddressCode") String userAddressCode);

    @Update("update user_address set " +
            "UserCode=#{userAddress.UserCode} ," +
            "ReceiveName=#{userAddress.ReceiveName} ," +
            "ReceivePhone=#{userAddress.ReceivePhone} ," +
            "ReceiveAddress=#{userAddress.ReceiveAddress} ," +
            "AreaCode=#{userAddress.AreaCode} ," +
            "AreaName=#{userAddress.AreaName} ," +
            "AreaNamePath=#{userAddress.AreaNamePath} ," +
            "DefaultStatus=#{userAddress.DefaultStatus} " +
            "where UserAddressCode=#{userAddress.UserAddressCode} ")
    void update(@Param("userAddress") UserAddress userAddress);

    @Select("select  * from user_address " +
            "where " +
            "UserCode = #{userCode}  order by DefaultStatus desc ")
    List<UserAddress> getMyAddressList(@Param("userCode") String userCode);

    @Delete("delete from user_address where UserAddressCode=#{userAddressCode}")
    int delete(@Param("userAddressCode")String userAddressCode);

    //修改默认地址
    @Update("update user_address set DefaultStatus=0 where UserCode=#{userCode}")
    int addressSetDefault0(@Param("userCode")String userCode);

    //修改默认地址
    @Update("update user_address set DefaultStatus=1 where UserAddressCode=#{userAddressCode}")
    int addressSetDefault1(@Param("userAddressCode")String userAddressCode);

}
